const App = getApp();

const func = require('../../utils/func.js');
Page({
	data: {
		grade_status: 1, //改为1的时候是一级分类    改为2的时候是二级分类     改为3的时候是三级分类
		height: 0, // 左侧筛选栏
		firstTab: 0, //左侧筛选栏默认值(一级栏目选中 数组键值)
		secondTab: 0, //二级栏目选中 数组键值
		typeid: 0, //当前选中的栏目id(一二级栏目时用于查询商品列表)
		typename: '',
		scrollViewId: "id_0", //
		tabbar: [], //栏目列表
		product_list: {
			total: 0
		}, //产品列表
		page:1,
		isLoading:true,
		no_more:false,
	},

	onLoad(options) {
		let _this = this
		setTimeout(() => {
			wx.getSystemInfo({
				success: (res) => {
					this.setData({
						height: res.windowHeight - res.windowWidth / 750 * 92
					})
				},
			})
		})
		_this.getPageData();
	},

	getPageData: function() {
		let _this = this;
		App._requestApi(_this, App.globalData.config.apiIndexUrl, {
			apiAllType_1: `ekey=1`
		}, function(res) {
			if (res.data.cart_total_num > 0) {
				wx.setTabBarBadge({
					index: 2,
					text: String(res.data.cart_total_num )
				});
			} else if(res.data.cart_total_num == 0){
			  wx.removeTabBarBadge({
			        index: 2
			    });
			}
			let tabbar = res.data.apiAllType[1].type_list,
				product_list = res.data.apiAllType[1].goods_list,
				typeid = res.data.apiAllType[1].typeid,//第一个typeid
                typename = res.data.apiAllType[1].typename,//第一个typename
                grade_status = res.data.apiAllType[1].type;//后台设置的分类风格
			_this.setData({
				tabbar,
				product_list,
				typeid,
                typename,
				grade_status,
				isLoading: false
			});
		});
	},
	toFix(price){
		func.toFix(price)
	},
	/**
	 * 加载页面数据
	 */
	getGoodData(isPage, page) {
		let _this = this,
			typeid = _this.data.typeid;
		if (0 == typeid) {
			_this.setData({
				product_list: {
					total: 0
				}
			})
			return;
		}
		page = page || 1;
		let field = `ekey=1&seckill=on&pagesize=6&page=` + page + `&typeid=` + typeid;
		App._requestApi(_this, App.globalData.config.apiIndexUrl, {
			apiList_1: field,
		}, function(res) {
			let product_list = res.data.apiList[1]; // 第一个list文档列表数据
			if (isPage == true) {
				let dataList = _this.data.product_list;
				_this.setData({
					'product_list.data': dataList.data.concat(product_list.data),
					isLoading: false
				})
			} else {
				_this.setData({
					product_list,
					isLoading: false
				})
			}
		});
	},
	// 点击左侧筛选
	switchNav(e) {
		let _this = this;
		let cur = e.currentTarget.dataset.current;
		if (_this.data.firstTab == cur) {
			return false;
		} else {
			let secondTab = 0;
			let typeid = 0,
				typename = '';
			if (1 == _this.data.grade_status) {
				typeid = _this.data.tabbar[cur].id;
				typename = _this.data.tabbar[cur].typename;
			} else {
				if (0 < _this.data.tabbar[cur].child.length) {
					typeid = _this.data.tabbar[cur].child[0].id;
					typename = _this.data.tabbar[cur].child[0].typename;
				}
			}
			_this.setData({
				firstTab: cur,
				secondTab,
				typeid,
				typename,
				page:1
			}, () => {
				_this.checkCor()
			})
			_this.getGoodData();
		}
	},
	// 二级导航切换
	changeNav(e) {
		let _this = this
		let index = e.currentTarget.dataset.index,
			typeid = e.currentTarget.dataset.id,
			typename = _this.data.tabbar[_this.data.firstTab].child[index].typename;

		_this.setData({
			secondTab: index,
			typeid,
			typename,
			page:1
		});
		_this.getGoodData();
	},
	//判断当前滚动超过一屏时，设置tab标题滚动条。
	checkCor() {
		let _this = this;
		if (_this.data.firstTab > 5) {
			_this.setData({
				scrollViewId: `id_${_this.data.firstTab - 2}`
			})
		} else {
			_this.setData({
				scrollViewId: 'id_0'
			})
		}
	},
	/**
	 * 下拉刷新
	 */
	onPullDownRefresh: function() {
		this.getPageData(); // 获取首页数据
		wx.stopPullDownRefresh(); // 停止下拉刷新
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachbottom() {
		let _this = this;
		if (_this.data.page >= _this.data.product_list.last_page) {
			_this.setData({
				no_more: true
			});
			return false;
		}
		_this.getGoodData(true, ++_this.data.page);
	},
	// 跳转到其他界面
	jumpToView(e) {
		func.jumpToUrl(e)
	}
})
